package com.nd.OSS;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.UUID;


public class AliyunLetter {
    //传入短信


    public static String sendMsg(String phoneNumber) throws JsonProcessingException {
        String accessKeyId = "LTAI5tSHBBV5b1PkUocrkr4M";
        String accessKeySecret = "shGJrj1mxSK0jcgGy5WDtgTMabyvo4";
        String signName = "stone4j";
        String templateCode = "SMS_173765187";

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);

        //发送短信的信息
        String code = UUID.randomUUID().toString().substring(0, 6);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            //发送短信
            CommonResponse response = client.getCommonResponse(request);
//            String data = response.getData();
//            System.out.println(data);
//            System.out.println(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //发送失败,null,发送成功将验证码数据返回
        return code;
    }
}